package com.ciei.dpagm.service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ciei.dpagm.common.service.BaseService;
import com.ciei.dpagm.entity.Area;
import com.ciei.dpagm.mapper.AreaMapper;
import com.ciei.dpagm.util.APP;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class AreaService extends BaseService<AreaMapper,Area> {


    public JSONObject loadAreaVue(JSONObject jsonObject) {
        JSONObject json = new JSONObject();
        JSONArray jsonArray=new JSONArray();
        List<Area> areas;
        if(jsonObject.getInteger("areaId")==null){
            //值为空代表初始化，应该查询最高等级的数据，目前只适用于一个最高等级的数据，也就是中国
            List<Area> oneAreas = findByProperty("levelId", 0);
            Area oneArea = oneAreas.get(0);
            //筛选中国的省份数据
            areas = findByProperty("parentAreaId", oneArea.getAreaId());
        }else{
            areas = findByProperty("parentAreaId", jsonObject.getInteger("areaId"));
        }
        for(Area Area :areas){
            Map<String,Object> map = new HashMap<>();
            map.put("value",Area.getAreaId());
            map.put("label",Area.getAreaName());
            jsonArray.add(map);
        }
        json.put(APP.DATA,jsonArray);
        return json;
    }
}
